﻿using Common.AspNetCore;
using Common.Infrastructure;
using Log.Api.Extensions;
using Log.Api.LogModels;
using Log.Api.QueryModel;
using Log.Api.Service;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Common.AspNetCore
{
    [Route("api/PackScanLog")]
    public class PackScanLogController : ApiController
    {
        private PackScanLogService packScanContext;

        public PackScanLogController(IServiceProvider provider, PackScanLogService context)
            : base(provider)
        {
            this.packScanContext = context;
        }

        /// <summary>
        /// 新增或者修改
        /// </summary>
        /// <param name="sysLogModel"></param>
        [HttpPost("AddLog")]
        public async Task<ApiActionResult<bool>> AddLog([FromBody] PackScanLogModel sysLogModel)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                return await packScanContext.InsertAsync(sysLogModel);
            });
        }

        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="id"></param>
        [HttpPost("DeleteLog")]
        public ApiActionResult<bool> DeleteLog([FromBody] PackScanLogQuery query)
        {
            return SafeExecute<bool>((param) =>
            {
                return packScanContext.DeleteByQuery(query);
            });
        }

        /// <summary>
        /// 获取包装扫码
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost("GetLogs")]
        public ApiActionResult<List<PackScanLogModel>> GetLogs([FromBody] PackScanLogQuery query)
        {
            return SafeExecute<List<PackScanLogModel>>((param) =>
            {
                return packScanContext.GetLogs(query);
            });
        }

        /// <summary>
        /// 获取包装扫码数量
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost("GetTotalCount")]
        public ApiActionResult<long> GetTotalCount([FromBody] PackScanLogQuery query)
        {
            return SafeExecute<long>((param) =>
            {
                return packScanContext.GetTotalCount(query);
            });
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost("GetLogPage")]
        public ApiActionResult<ApiPagedResult<PackScanLogModel>> GetLogPage([FromBody] PackScanLogQuery query)
        {
            return SafeExecute<ApiPagedResult<PackScanLogModel>>((param) =>
            {
                ApiPagedResult<PackScanLogModel> ret = new ApiPagedResult<PackScanLogModel>();
                if (query.BeginTime == null)
                {
                    query.BeginTime = DateTime.Now.AddDays(-7).Date;
                }

                if (query.EndTime == null)
                {
                    query.EndTime = DateTime.Now.AddDays(1).Date;
                }

                query.BeginTime = ElasticClientExtension.GetDateForEs(query.BeginTime.Value);
                query.EndTime = ElasticClientExtension.GetDateForEs(query.EndTime.Value);
                ret.Data = packScanContext.GetLogPage(query);
                var TotalCount = (int)packScanContext.GetTotalCountByPage(query);
                ret.TotalCount = TotalCount < 0 ? 0 : TotalCount;
                return ret;
            });
        }

        /// <summary>
        /// 根据条件获取日志汇总数据
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost("GetTotalLogs")]
        public ApiActionResult<PackScanTotalLogModel> GetTotalLogs([FromBody] PackScanLogQuery query)
        {
            return SafeExecute((param) =>
            {
                return packScanContext.GetTotalLogs(query);
            });
        }
    }
}
